home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / math / gle-3.000 / gle-3 / gle / memory.c < prev    next >
C/C++ Source or Header  |  1995-02-07  |  3KB  |  145 lines

  1. #include "all.h"
  2. #ifndef __TURBOC__
  3. #define farcalloc calloc
  4. #else
  5. #include <alloc.h>
  6. #endif
  7. #ifdef unix
  8. #include <malloc.h>
  9. #endif
  10. extern char errgle[];
  11. int free_savemem(void);
  12. char *sdup(char *s)
  13. {
  14.     char *v;
  15.     v = myalloc(strlen(s)+1);
  16.     strcpy(v,s);
  17.     return v;
  18. }
  19. char *save_memory;
  20. free_savemem()
  21. {
  22.     if (save_memory!=NULL) free(save_memory);
  23.     save_memory = 0;
  24. }
  25. init_memory()
  26. {
  27.     save_memory = malloc(1000);
  28.     if (save_memory == NULL) gle_abort("Not able to allocate save memory\n");
  29. }
  30. static int32 totalmem,worst;
  31. void myfree(void *p)
  32. {
  33.     myfrees(p,"UNKNOWN");
  34. }
  35. void myfrees(void *p,char *s)
  36. {
  37.     static int32 *l;
  38.  
  39.  
  40.     l = (int32 *) p;
  41.     totalmem -= *(--l);
  42.     if (*(--l)!=1234) {
  43.         sprintf(errgle,"Free memory (%s) not mine %ld  %ld \n",s,*l,*(++l));
  44.         gle_abort(errgle);
  45.     }
  46.     free(l);
  47.     return;
  48. }
  49. void *myallocn(int32 nitems,int32 size)
  50. {
  51.     return myallocz(nitems*size);
  52. }
  53.  
  54. void *myalloc(int32 size)
  55. {
  56.     static void *p;
  57.     static int32 *l;
  58.  
  59.     if (size==0) {
  60.         sprintf(errgle," error, attempt to allocate ZERO memory \n");
  61.         gle_abort(errgle);
  62.     }
  63.     if (size>40000) gprint(" allocating a lot of memory %d \n",size);
  64.     p = malloc(size+sizeof(int32)*2);
  65.     if (p==NULL) {
  66.         freeafont();
  67.         p = malloc(size+sizeof(int32)*2);
  68.         if (p==NULL) {
  69.          free_savemem();
  70.          sprintf(errgle,"\n Memory allocation failure (size %d)  \n"
  71.             ,size);
  72.          gle_abort(errgle);
  73.         }
  74.     }
  75.     l = (int32 *) p;
  76.     *l++ = 1234;
  77.     *l = size+sizeof(int32)*2;
  78.     totalmem += *l;
  79.     if (totalmem>worst) worst = totalmem;
  80.     p = (void *) ++l;
  81.  
  82.     return p;
  83. }
  84. void *myallocz(int32 size)
  85. {
  86.     static void *p;
  87.     static int32 *l;
  88.     if (size==0) {
  89.         free_savemem();
  90.         sprintf(errgle," zerror, attempt to allocate ZERO memory \n");
  91.         gle_abort(errgle);
  92.     }
  93.     if (size>40000) {
  94.       gprint("(z) allocating a lot of memory %ld \n",size);
  95.     }
  96.     p = farcalloc(1,size+sizeof(int32)*2);
  97. /*    p = farcalloc((size+8)/4,4);*/
  98.     if (p==NULL) {
  99.        freeafont();
  100.        p = farcalloc(1,size+sizeof(int32)*2);
  101. /*        if (p==NULL) {
  102.         free_cache();
  103.         gprint("Freeing cached characters\n");
  104.         p = farcalloc(1,size+sizeof(int32)*2);
  105.        }
  106. */
  107.        if (p==NULL) {
  108.         free_savemem();
  109.         sprintf(errgle,"\n\n (z) Memory allocation failure %d  \n\n"
  110.             ,size);
  111.         gle_abort(errgle);
  112.        }
  113.     }
  114.     l = (int32 *) p;
  115.     *l++ = 1234;
  116.     *l = size+sizeof(int32)*2;
  117.     totalmem += *l;
  118.     if (totalmem>worst) worst = totalmem;
  119.     p = (void *) ++l;
  120.     return p;
  121. }
  122. int32 mem_total()
  123. {
  124.     return totalmem;
  125. }
  126. int32 mem_worst()
  127. {
  128.     return worst;
  129. }
  130.  
  131. int32 gtotalmem;
  132. #ifdef __TURBOC__
  133. #include <alloc.h>
  134. void far * far _graphgetmem(unsigned size)
  135. {
  136.     return(myalloc(size));
  137. }
  138. void far _graphfreemem(void far *ptr, unsigned size)
  139. {
  140.     myfree(ptr);
  141. }
  142.  
  143. #endif
  144.  
  145.